package acom.test.swing.bb;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ResultPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JLabel title_lb;
	private JButton out_but;
	private JFileChooser fileChooser = null;

	private JScrollPane scrollPane;
	private JTable table = null;
	private DefaultTableModel defaultModel = null;

	public ResultPanel() {
		this.setLayout(null);
		title_lb = new JLabel("中奖名单");
		
		out_but = new JButton("导出结果");
		fileChooser = new JFileChooser("D:\\");
		

		Object[][] p = {
				{ "王鹏", new Integer(91), new Integer(1949), new Integer(1910) },
				{ "朱雪莲", new Integer(82), new Integer(1969), new Integer(1510) } };
		String[] n = { "中奖号码", "姓    名", "手机号码", "地                址" };
		defaultModel = new DefaultTableModel(p, n);// 创建一个默认的表格模型
		table = new JTable(defaultModel);

		
		
		scrollPane = new JScrollPane(table);
		
		init();
		this.add(title_lb);
		this.add(out_but);
		this.add(scrollPane);
	}

	private void init() {
		this.setBackground(Color.WHITE);
		title_lb.setBounds(250, 10, 200, 25);
		out_but.setBounds(550, 10, 80, 25);
		
		scrollPane.setBounds(10, 50, 670, 360);
		title_lb.setFont(new Font("楷体",Font.PLAIN,30));
		
		//table.setFont(new Font("楷体",Font.PLAIN,28));
		
		table.setPreferredScrollableViewportSize(new Dimension(400, 80));

		table.setShowGrid(false);
		table.getTableHeader().setVisible(false);

		TableColumn fourColumn = table.getColumnModel().getColumn(3);
		fourColumn.setPreferredWidth(200);
		
		out_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				Out_Action();
			}
		});
	}

	

	public void clearTable() {
		for (int index = table.getModel().getRowCount() - 1; index >= 0; index--) {
			defaultModel.removeRow(index);
		}

	}

	private AnswerPeople people;
	private List<AnswerPeople> peoples;

	public void showPrizesResult(Prize prize) {

		peoples = prize.getPersons();
		if (peoples.size() > 0) {
			// 增加 奖项 名称
			Vector<String> blank1 = new Vector<String>();
			blank1.add(" ");
			defaultModel.addRow(blank1);
			Vector<String> blank2 = new Vector<String>();
			blank1.add(" ");
			defaultModel.addRow(blank2);

			// 增加 奖项 名称
			Vector<String> vname = new Vector<String>();
			vname.add(prize.getPname());
			defaultModel.addRow(vname);

			// 增加 表格 头
			Vector<String> v = new Vector<String>();
			v.add("中奖号码");
			v.add("姓    名");
			v.add("手机号码 ");
			v.add("地            址");
			defaultModel.addRow(v);

			// 增加表格 内容
			for (int i = 0; i < peoples.size(); i++) {
				people = peoples.get(i);
				Vector<String> vpeople = new Vector<String>();
				vpeople.add(people.getPaperId());
				vpeople.add(people.getPeopleName());
				vpeople.add(people.getPeoTel());
				vpeople.add(people.getAddress());
				defaultModel.addRow(vpeople);
			}

		}

	}
	
	
	protected void Out_Action() {
		
		int result = fileChooser.showSaveDialog(this);
		File file = null;
		String fileName;
		//当用户没有选择文件,而是自己键入文件名称时,系统会自动以此文件名建立新文件.
		if (result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			//System.out.println("您选择打开的文件名称为：" + file.getName());
			if(!SystemUtil.checkFileExtendName(file.getName(), "txt")){
				JOptionPane.showMessageDialog(null, "请选择txt文件，或新建一个txt");
				return;
			}
		} else if (result == JFileChooser.CANCEL_OPTION) {
			
		}
		//写入文件使用FileOutputStream,在这个范例中,写入文件的方式是将之前内容清除并重新写入.若想把新增的内容加在原有的文件内容后面，可以使用FileOutputStream(String
		//name,Boolean append)这个构造函数.
		BufferedWriter out = null;
		if (file != null) {
			try {
				out =new BufferedWriter(
						new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8"));
			} catch (FileNotFoundException fe) {
				return;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String content = getResultContent();
			try {
				out.write(content);
			} catch (IOException ioe) {
				
			} finally {
				try {
					if (out != null)
						out.close();
				} catch (IOException ioe2) {
				}
			}
		}		
	}
	
	private String getResultContent(){
		List<Prize> prizes=SystemUtil.getPrizelist();
		
		StringBuffer content = new StringBuffer();
		content.append("\r\n");
		content.append("生成以下数据的时间："+SystemUtil.getNowTime()+"\r\n");
		
		for(int i=prizes.size()-1;i>=0;i--){
			Prize priz=prizes.get(i);
			
			peoples = priz.getPersons();
			if (peoples.size() > 0) {
				content.append("\r\n");
				content.append("\r\n");
				content.append(priz.getPname()+"\r\n");
				content.append("序号    中奖号码      姓名     手机号码      地 址"+"\r\n");
				
				for (int j = 0; j < peoples.size(); j++) {
					people = peoples.get(j);
					content.append((j+1));
					content.append("    "+people.getPaperId());
					content.append("     "+people.getPeopleName());
					content.append("     "+people.getPeoTel());
					content.append("    "+people.getAddress()+"\r\n");
				}
			}
			
		}
		
		return content.toString();
	}

}
